package com.wny.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
//import java.util.HashMap;
//import java.util.Map;
import java.util.concurrent.Executors;

public class SocketServer {
	static DBOperatorWrapper dao;

	private static int PORT = 8888;

	// define enum
	public enum ACT {
		add, del, update, get, other, end, getid, loc;
	}
	
	private static class myExecutor implements Runnable
	{
		Socket socket;
		public myExecutor(Socket s) {
			// TODO Auto-generated constructor stub
			socket = s;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			boolean writeback = true;
			try {
				System.out.println("Connection accepted: " + socket);
				BufferedReader in = new BufferedReader(new InputStreamReader(
						socket.getInputStream()));

				PrintWriter out = new PrintWriter(new BufferedWriter(
						new OutputStreamWriter(socket.getOutputStream())), true);

				while (true) {
					writeback = true;
					String message = in.readLine();
					if (message==null || message.equals("END")) {
						break;
					} else {
						String feedback = " ";
						String retrieve = new String();
						String[] request = message.split("&");
						String[] reHead = request[0].split("=");
						ACT act = ACT.valueOf(reHead[0]);

						switch (act) {
						
						case add:
							if(dao.hasEntity(reHead[1], request))
								feedback = "0";
							else
								feedback = dao.insert(reHead[1], request);							
							break;
						case del:
							feedback = dao.delete(reHead[1], request);
							break;
						case update:
							feedback = dao.update(reHead[1], request);
							break;
						case get:
							retrieve = dao.retrieve(reHead[1], request);
							break;
						case getid:
							retrieve=dao.getid(reHead[1],request);
							break;
						case other:
							feedback = dao.otheract(reHead[1], request);
							break;
						case loc:
							dao.updateLocation(request[1], request[2], request[3]);
							writeback = false;
						default:
							break;
						}

						if (" " != feedback && writeback)
							out.println(feedback);
						else if (writeback)
							out.println(retrieve);
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				System.out.println("Socket with client closing...");
				try {
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	public static void main(String[] args) throws IOException {

		String dbHost = "www.db4free.net";
		String dbPort = "3306";
		String dbName = "whosnearyou";
		String dbUserName = "wnyroot";
		String dbPassword = "wnyroot";
		ExecutorService exec = Executors.newFixedThreadPool(4);
		dao = new DBOperatorWrapper(dbHost, dbPort, dbName,
				dbUserName, dbPassword);
		boolean flag = dao.Connect();
		if (!flag)
			return;

		ServerSocket serverSocket = new ServerSocket(PORT);
		System.out.println("Server Started: " + serverSocket);
		while(true)
		{
			Runnable myExec = new myExecutor(serverSocket.accept());
			exec.submit(myExec);
		}
	}
}